package com.younion.action.manage;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.google.common.collect.Maps;
import com.younion.business.IRegisterUserService;
import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;
import com.younion.util.MD5;
import com.younion.vo.RegisterUserVO;

import cpcns.jhsks.util.MapCollectionUtils;

/**
 * @author wells
 */
public class RegisterUserAction extends BaseAction {
	
	public static final String mobilePattern = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory
			.getLog(RegisterUserAction.class);

	private IRegisterUserService registerUserService = null;

	private RegisterUserVO registerUserVO = new RegisterUserVO();

	private List<RegisterUserVO> registerUserList;// 结果集列表

	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("listRegisterUser")
	public String listRegisterUser() {
		try {
			Map paramMap = null;
			if (registerUserVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(registerUserVO));
			}
			// 获取总记录数
			int total = registerUserService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			registerUserList = registerUserService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
			this.addLog(2, "查询注册用户列表", "查询注册用户列表", this.getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addRegisterUser")
	public String addRegisterUser() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editRegisterUser")
	public String editRegisterUser() {
		try {
			registerUserVO = registerUserService.getById(registerUserVO.getId()
					.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveRegisterUser")
	public String saveRegisterUser() {
		try {
			Pattern p = Pattern.compile(mobilePattern);
	        if (StringUtils.isNotBlank(registerUserVO.getLoginId()) && !p.matcher(registerUserVO.getLoginId()).matches()) {
	            this.tipMessage = "登录名必须是手机号";
	            return ERROR;
	        }
	        Map paraMap = Maps.newHashMap();
	        paraMap.put("loginId", registerUserVO.getLoginId());
	        Collection exists = registerUserService.getByMap(paraMap);
	        if(exists.size() > 0&&registerUserVO.getId()==null){//非编辑状态提示
	        	this.tipMessage = "该登录名已存在，不能重复注册";
	            return ERROR;
	        }
			if (StringUtils.isEmpty(registerUserVO.getPassword())) {// 如果密码为空则默认为123456
				registerUserVO.setPassword("123456");
			}
			registerUserVO.setPassword(MD5.getMD5(registerUserVO.getPassword()
					.getBytes("utf-8")));
			registerUserVO = registerUserService.save(registerUserVO);
			String id = registerUserVO.getId() == null ? "" : registerUserVO.getId().toString();
			String log = "".equals(id) ? "添加注册用户" : "更新注册用户";
			int type = "".equals(id) ? 3:4;
			this.addLog(type, log+id, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "registerUserList");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteRoles")
	public String deleteRegisterUser() {
		try {
			String pkId = request.getParameter("ids");
			registerUserService.remove(Arrays.asList(pkId.split(",")));
			String log = "删除注册用户";
			this.addLog(5, log+pkId, log,getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("refreshNavTabId", "registerUserList");
		return SUCCESS;
	}

	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewRegisterUser")
	public String viewRegisterUser() {
		try {
			registerUserVO = registerUserService.getById(registerUserVO.getId().toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public IRegisterUserService getRegisterUserService() {
		return registerUserService;
	}

	public void setRegisterUserService(IRegisterUserService registerUserService) {
		this.registerUserService = registerUserService;
	}

	public RegisterUserVO getRegisterUserVO() {
		return registerUserVO;
	}

	public void setRegisterUserVO(RegisterUserVO registerUserVO) {
		this.registerUserVO = registerUserVO;
	}

	public List<RegisterUserVO> getRegisterUserList() {
		return registerUserList;
	}

	public void setRegisterUserList(List<RegisterUserVO> registerUserList) {
		this.registerUserList = registerUserList;
	}

}